using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.SceneManagement;

public class FirstSceneCubeControl : MonoBehaviour
{
    public GameObject StandardCubePre;

    List<GameObject> cubes;
    int oldCubeCount;

    private void Start()
    {
        cubes = new List<GameObject>();
        oldCubeCount = cubes.Count;
        CreateCube();
    }

    private void Update()
    {
        var empty = cubes.Where(t => t == null).ToList();
        if (empty.Any())
            empty.ForEach(t => cubes.Remove(t));

        if (cubes.Count <= 0)
        {
            SceneManager.LoadScene("SecondScene");
            return;
        }

        oldCubeCount = cubes.Count;
        BasicControl.CheckCubeMove(cubes);
    }

    void CreateCube()
    {
        var index = 0;
        for (float y = -1; y < 1; y++)
        {
            for (float x = -1; x < 1; x++)
            {
                for (float z = -1; z < 1; z++)
                {
                    var cube = Instantiate(StandardCubePre, new Vector3(x, y, z), Quaternion.identity);//,transform);                    

                    BasicControl.CubeRotate(cube);

                    cube.name = $"Standard Cube {index++}";

                    cubes.Add(cube);
                }
            }
        }
    }
}
